import java.util.ArrayList;

public class State {

	int id;

	ArrayList<ArrayList<State>> next;

	String tokenName;
	
	boolean accepting;
	
	static int stateNum = 0;
	static int NUM_CHARS = 256;

	public State() {

		this.id = stateNum;
		stateNum++;
		next = new ArrayList<ArrayList<State>>();
		for (int i = 0; i < NUM_CHARS; i++) {
			next.add(new ArrayList<State>());
		}
		this.tokenName = null;
	}
	
	public void setAccepting(boolean a){
		accepting=a;
	}
	
	public void setName(String s){
		tokenName = s;
	}
	
	public boolean isAccepting(){
		return accepting;
	}

	public void addTransition(int startIndex, int endIndex, State destination) {
		for (int i = startIndex; i <= endIndex; i++) {
			next.get(i).add(destination);
		}
	}

	public void addTransition(int startIndex, int endIndex, State destination,
			Rule r) {
		if (r == null)
			addTransition(startIndex, endIndex, destination);
		else
			for (int i = startIndex; i <= endIndex; i++) {
				if (r.matchString(String.valueOf((char) i)))
					next.get(i).add(destination);
			}
	}

	public void addTransition(int startIndex, int endIndex,
			ArrayList<State> destinations) {
		for (int i = startIndex; i <= endIndex; i++) {
			next.get(i).addAll(destinations);
		}
	}
	
	public void removeTransition(int startIndex,int endIndex, State destination){
		for (int i = startIndex; i <= endIndex; i++) {
			next.get(i).remove(destination);
		}
		
	}

	public void setId(int i) {
		id = i;
	}

	public ArrayList<State> getNext(int c) {
		if (c < 0 || c > 255) {
			c = 0;
		}
		return next.get(c);
	}

	public int getId() {
		return id;
	}

	public String getTokenName() {
		return tokenName;
	}

	public void copyTransitionsFrom(State s) {
		for (int i = 0; i < NUM_CHARS; i++) {
			for (int j = 0; j < s.next.get(i).size(); j++) {
				next.get(i).add(s.next.get(i).get(j));
			}
		}
	}

}
